//
//  MateListTableViewCell.m
//  TQY
//
//  Created by 甘世清 on 2024/10/2.
//

#import "MateListTableViewCell.h"
#import "MateListCollectionViewCell.h"
#import "MateReportPopView.h"
#import "MatePEopleListViewController.h"
#import "AddMatePopView.h"
#import "MateAddressMapViewController.h"
@implementation MateListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    UIView * imageBgView = [[UIView alloc]init];
    imageBgView.layer.cornerRadius = 40;
    imageBgView.layer.masksToBounds = YES;
    imageBgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:imageBgView];
    [imageBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(120);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
        make.height.mas_equalTo(100);
        make.top.equalTo(self.contentView).offset(10);
    }];
    
    UIView * bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 30;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];

    [self.contentView addSubview:self.typeImageView];
    [self.typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(100);
        make.left.equalTo(self.contentView).offset(24);
        make.top.equalTo(self.contentView).offset(10);
    }];

    [self.contentView addSubview:self.titleTopLabel];
    [self.titleTopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageBgView).offset(16);
        make.right.equalTo(imageBgView.mas_right).offset(-60);
        make.top.equalTo(imageBgView).offset(25);
    }];

    [self.typeImageView addSubview:self.typeLabel];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.typeImageView);
        make.height.mas_equalTo(24);
    }];
    [self.contentView addSubview:self.messageLabel];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.titleTopLabel);
          make.top.equalTo(self.titleTopLabel.mas_bottom).offset(10);
    }];

    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.typeImageView);
        make.top.equalTo(self.typeImageView.mas_bottom).offset(10);
    }];
    UIImageView * mateimage = [[UIImageView alloc]init];
    mateimage.image = SQGetImage(@"shijian");
    [self.contentView addSubview:mateimage];
    [mateimage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(12);
            make.left.equalTo(self.messageLabel);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
    }];

    [self.contentView addSubview:self.mateTimeLabel];
    [self.mateTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.messageLabel.mas_right);
        make.left.equalTo(mateimage.mas_right).offset(6);
        make.centerY.equalTo(mateimage);
        make.height.mas_equalTo(20);
    }];

    UIImageView * addressImage = [[UIImageView alloc]init];
    addressImage.image = SQGetImage(@"dizhi");
    [self.contentView addSubview:addressImage];
    [addressImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(12);
        make.left.equalTo(self.messageLabel);
        make.top.equalTo(mateimage.mas_bottom).offset(10);
    }];

    [self.contentView addSubview:self.mateAddressLabel];
    [self.mateAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.messageLabel.mas_right);
        make.left.equalTo(addressImage.mas_right).offset(6);
        make.centerY.equalTo(addressImage);
        make.height.mas_equalTo(20);
    }];
    UIButton * addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addressBtn.backgroundColor = UIColor.clearColor;
    [addressBtn addTarget:self action:@selector(SQ_addressBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:addressBtn];
    [addressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.mateAddressLabel);
    }];
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = SQColorF5F5F5;
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1);
            make.left.equalTo(self.contentView).offset(24);
            make.right.equalTo(self.contentView.mas_right).offset(-24);
            make.top.equalTo(self.mateAddressLabel.mas_bottom).offset(14);
    }];
    [self.contentView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom);
        make.height.mas_equalTo(50);
        make.left.equalTo(lineView);
    }];

    [self.collectionView registerClass:[MateListCollectionViewCell class] forCellWithReuseIdentifier:@"MateListCollectionViewCell"];
    self.collectionView.alwaysBounceVertical = NO;
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.numberLabel.mas_right);
        make.top.equalTo(lineView.mas_bottom);
        make.height.mas_equalTo(50);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
    }];


    UIView * lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = SQColorF5F5F5;
    [self.contentView addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(1);
            make.left.equalTo(self.contentView).offset(24);
            make.right.equalTo(self.contentView.mas_right).offset(-24);
        make.top.equalTo(self.collectionView.mas_bottom);
    }];
    [self.contentView addSubview:self.moreBtn];
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(imageBgView.mas_right);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(40);
            make.centerY.equalTo(self.titleTopLabel);
    }];
    WS(weakSelf);
    [self.moreBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_reportButtonClick];
    }];

    [self.contentView addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-32);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(36);
            make.top.equalTo(lineView2.mas_bottom).offset(10);
    }];
    [self.addBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if (weakSelf.model.joinStatus == 0) {
            [weakSelf SQ_addMateButtonClick];
        } else  {
            [weakSelf SQ_deletedMateButtonClick];
        }

    }];

    [self.contentView addSubview:self.distanceLabel];
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.addBtn.mas_left).offset(-10);
            make.left.equalTo(lineView2);
            make.centerY.equalTo(self.addBtn);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-12);
        make.top.equalTo(self.contentView).offset(50);
        make.bottom.equalTo(self.addBtn.mas_bottom).offset(10);
    }];
    
    self.FS_cellBottomView = self.addBtn;
}

- (void)setModel:(MateModel *)model{
    _model = model;
    self.messageLabel.text = model.content;
    [self.typeImageView.headerImage sd_setImageWithURL:SQURLImage(model.portrait) placeholderImage:SQGetImage(@"1")];
    self.titleTopLabel.text = model.title;
    self.typeLabel.text = model.category;
    self.timeLabel.text = [NSString SQ_timeWithYearMonthDayCountDown:[NSString SQ_getTimeStrWithString:model.createTime]];
   
    self.mateTimeLabel.text = model.time;
    self.mateAddressLabel.text = @"活动地址,点击查看";
    if ([model.userType isEqualToString:@"0"]) {
        self.numberLabel.text = [NSString stringWithFormat:@"总人数:%@/%@",model.joins,model.userCount];
    } else {
        self.numberLabel.text = [NSString stringWithFormat:@"男%@/%@ 女%@/%@",model.joinMan,model.manCount,model.joinWoman,model.womanCount];
    }
    if ([[UserModel share].userid isEqualToString:model.uid]) {
        self.addBtn .hidden = YES;
        self.moreBtn.hidden = YES;
    } else {
        self.addBtn .hidden = NO;
        self.moreBtn.hidden = NO;
    }
    if (self.model.distance > 0) {
        if (model.distance <1000) {
            self.distanceLabel.text = [NSString stringWithFormat:@"距离您:%.fm",model.distance ];
        } else  {
            self.distanceLabel.text = [NSString stringWithFormat:@"距离您:%.fkm",model.distance/1000.f];
        }
    } else {
        self.distanceLabel.text = @"";
    }
    [self SQ_addButtonUIView];
}
-(void)SQ_addButtonUIView{
    _addBtn.SQBackimageView.hidden = YES;
    if (self.model.joinStatus == 0) {
        _addBtn.text = SQNSLocalString(@"搭一手");
        _addBtn.backgroundColor = SQColor3DA59D;
        _addBtn.SQBackimageView.hidden = NO;
    } else if(self.model.joinStatus ==1) {
        self.addBtn.text = SQNSLocalString(@"取消申请");
        self.addBtn.backgroundColor = SQHexColor(@"#999999", 0.5);
    }else  {
        _addBtn.text = SQNSLocalString(@"退出活动");
        self.addBtn.backgroundColor = SQHexColor(@"#999999", 0.5);
    }
}
-(void)SQ_addressBtnClick{
    MateAddressMapViewController * vc = [[MateAddressMapViewController alloc]init];
    vc.model = self.model;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}


-(void)SQ_headerViewButtonClick{
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = self.model.uid;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}

-(void)SQ_reportButtonClick{
    MateReportPopView * vc = [[MateReportPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 260+FootHeight)];
    vc.model = self.model;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
  
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    [vc.moreBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setHideMatetouchBlock:^{
        if (weakSelf.touchBlock) {
            weakSelf.touchBlock(weakSelf.model);
        }
        [wk_pop dismiss];
    }];
}
-(void)SQ_addMateButtonClick{
    AddMatePopView * vc = [[AddMatePopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 300+FootHeight)];
    vc.model = self.model;
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    pop.avoidKeyboardSpace = -10;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [vc setAddMatetouchBlock:^{
        [wk_pop dismiss];
        weakSelf.model.joinStatus = 1;
        [weakSelf SQ_addButtonUIView];
    }];
}


//取消搭
-(void)SQ_deletedMateButtonClick{
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[UserModel share].userid forKey:@"uid"];
    [dic setObject:self.model.userid forKey:@"id"];
    [HttpTool post:@"lug/leave" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        weakSelf.model.joinStatus = 0;
        [weakSelf SQ_addButtonUIView];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.model.users.count >5) {
        return 6;
    }
    return self.model.users.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MateListCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MateListCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.item > 4) {
        cell.gengduoLabel.hidden = NO;
    }else{
        cell.gengduoLabel.hidden = YES;
        cell.dic = [self.model.users objectAtIndex:indexPath.item];
    }
    cell.genderImage.hidden = !cell.gengduoLabel.hidden;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(45, 50);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 12, 0, 12);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MatePEopleListViewController * vc = [[MatePEopleListViewController alloc]init];
    vc.model = self.model;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (UILabel *)distanceLabel{
    if (!_distanceLabel) {
        _distanceLabel = [[UILabel alloc]init];
        _distanceLabel.textColor = SQColor666666;
        _distanceLabel.font = FONT_REGULAR(12);
    }
    return _distanceLabel;
}
- (SQCustomButton *)moreBtn{
    if (!_moreBtn) {
        _moreBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(30, 20) midmargin:0];
        _moreBtn.image = SQGetImage(@"gengduo");
        _moreBtn.backgroundColor = UIColor.clearColor;
    }
    return _moreBtn;
}

- (SQCustomButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _addBtn.text = SQNSLocalString(@"搭一手");
        _addBtn.textColor = UIColor.whiteColor;
        _addBtn.SQBackimageView.image = SQGetImage(@"矩形备份 7");
        _addBtn.font = FONT_REGULAR(12);
        _addBtn.backgroundColor = SQColor3DA59D;
        _addBtn.layer.cornerRadius = 18;
        _addBtn.layer.masksToBounds = YES;
    }
    return _addBtn;
}
- (UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc]init];
        _numberLabel.font = FONT_REGULAR(12);
        _numberLabel.textColor = SQColor333333;
        _numberLabel.backgroundColor = UIColor.clearColor;
    }
    return _numberLabel;
}

- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_REGULAR(14);
        _messageLabel.textColor = SQColor333333;
        _messageLabel.backgroundColor = UIColor.clearColor;
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}
- (UILabel *)titleTopLabel{
    if (!_titleTopLabel) {
        _titleTopLabel = [[UILabel alloc]init];
        _titleTopLabel.font = FONT_MEDIUM(16);
//        _titleTopLabel.textAlignment = NSTextAlignmentCenter;
        _titleTopLabel.adjustsFontSizeToFitWidth = YES;
        _titleTopLabel.textColor = SQColor333333;
        _titleTopLabel.backgroundColor = UIColor.clearColor;
        _titleTopLabel.numberOfLines = 0;
    }
    return _titleTopLabel;
}
- (UILabel *)typeLabel{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc]init];
        _typeLabel.font = FONT_MEDIUM(14);
        _typeLabel.textColor = UIColor.whiteColor;
        _typeLabel.textAlignment = NSTextAlignmentCenter;
        _typeLabel.backgroundColor = SQHexColor(@"BC99FF", 0.4);
        _typeLabel.numberOfLines = 0;
    }
    return _typeLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font =  FONT_REGULAR(12);
        _timeLabel.textColor = SQColor666666;
        _timeLabel.backgroundColor = UIColor.clearColor;
    }
    return _timeLabel;
}
- (UILabel *)mateTimeLabel{
    if (!_mateTimeLabel) {
        _mateTimeLabel = [[UILabel alloc]init];
        _mateTimeLabel.font =  FONT_REGULAR(12);
        _mateTimeLabel.textColor = SQColor333333;
        _mateTimeLabel.backgroundColor = UIColor.clearColor;
    }
    return _mateTimeLabel;
}
- (UILabel *)mateAddressLabel{
    if (!_mateAddressLabel) {
        _mateAddressLabel = [[UILabel alloc]init];
        _mateAddressLabel.font =  FONT_REGULAR(12);
        _mateAddressLabel.textColor = SQColor3DA59D;
        _mateAddressLabel.backgroundColor = UIColor.clearColor;
    }
    return _mateAddressLabel;
}
- (HeaderView *)typeImageView{
    if (!_typeImageView) {
        _typeImageView = [[HeaderView alloc]init];
        _typeImageView.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        _typeImageView.clipsToBounds = YES;
        [_typeImageView.btn addTarget:self action:@selector(SQ_headerViewButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _typeImageView.layer.cornerRadius = 20;
        _typeImageView.layer.masksToBounds = YES;
    }
    return _typeImageView;
}

@end
